﻿using DotNEToolkit.Extentions;
using ModengTerm.Terminal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ModengTerm.Terminal.Parsing
{
    /// <summary>
    /// 提供Xterm颜色索引值到RGB值的转换
    /// 
    /// 参考：
    /// http://www.calmar.ws/vim/256-xterm-24bit-rgb-color-chart.html
    /// https://vim.fandom.com/wiki/Xterm256_color_names_for_console_Vim
    /// </summary>
    public static class Xterm256Color
    {
        public static void ConvertRGB(int index, out byte r, out byte g, out byte b)
        {
            r = 0; g = 0; b = 0;
            switch (index)
            {
                case 0: r = 0; g = 0; b = 0; break;
                case 1: r = 128; g = 0; b = 0; break;
                case 2: r = 0; g = 128; b = 0; break;
                case 3: r = 128; g = 128; b = 0; break;
                case 4: r = 0; g = 0; b = 128; break;
                case 5: r = 128; g = 0; b = 128; break;
                case 6: r = 0; g = 128; b = 128; break;
                case 7: r = 192; g = 192; b = 192; break;
                case 8: r = 128; g = 128; b = 128; break;
                case 9: r = 255; g = 0; b = 0; break;
                case 10: r = 0; g = 255; b = 0; break;
                case 11: r = 255; g = 255; b = 0; break;
                case 12: r = 0; g = 0; b = 255; break;
                case 13: r = 255; g = 0; b = 255; break;
                case 14: r = 0; g = 255; b = 255; break;
                case 15: r = 255; g = 255; b = 255; break;
                case 16: r = 0; g = 0; b = 0; break;
                case 17: r = 0; g = 0; b = 95; break;
                case 18: r = 0; g = 0; b = 135; break;
                case 19: r = 0; g = 0; b = 175; break;
                case 20: r = 0; g = 0; b = 215; break;
                case 21: r = 0; g = 0; b = 255; break;
                case 22: r = 0; g = 95; b = 0; break;
                case 23: r = 0; g = 95; b = 95; break;
                case 24: r = 0; g = 95; b = 135; break;
                case 25: r = 0; g = 95; b = 175; break;
                case 26: r = 0; g = 95; b = 215; break;
                case 27: r = 0; g = 95; b = 255; break;
                case 28: r = 0; g = 135; b = 0; break;
                case 29: r = 0; g = 135; b = 95; break;
                case 30: r = 0; g = 135; b = 135; break;
                case 31: r = 0; g = 135; b = 175; break;
                case 32: r = 0; g = 135; b = 215; break;
                case 33: r = 0; g = 135; b = 255; break;
                case 34: r = 0; g = 175; b = 0; break;
                case 35: r = 0; g = 175; b = 95; break;
                case 36: r = 0; g = 175; b = 135; break;
                case 37: r = 0; g = 175; b = 175; break;
                case 38: r = 0; g = 175; b = 215; break;
                case 39: r = 0; g = 175; b = 255; break;
                case 40: r = 0; g = 215; b = 0; break;
                case 41: r = 0; g = 215; b = 95; break;
                case 42: r = 0; g = 215; b = 135; break;
                case 43: r = 0; g = 215; b = 175; break;
                case 44: r = 0; g = 215; b = 215; break;
                case 45: r = 0; g = 215; b = 255; break;
                case 46: r = 0; g = 255; b = 0; break;
                case 47: r = 0; g = 255; b = 95; break;
                case 48: r = 0; g = 255; b = 135; break;
                case 49: r = 0; g = 255; b = 175; break;
                case 50: r = 0; g = 255; b = 215; break;
                case 51: r = 0; g = 255; b = 255; break;
                case 52: r = 95; g = 0; b = 0; break;
                case 53: r = 95; g = 0; b = 95; break;
                case 54: r = 95; g = 0; b = 135; break;
                case 55: r = 95; g = 0; b = 175; break;
                case 56: r = 95; g = 0; b = 215; break;
                case 57: r = 95; g = 0; b = 255; break;
                case 58: r = 95; g = 95; b = 0; break;
                case 59: r = 95; g = 95; b = 95; break;
                case 60: r = 95; g = 95; b = 135; break;
                case 61: r = 95; g = 95; b = 175; break;
                case 62: r = 95; g = 95; b = 215; break;
                case 63: r = 95; g = 95; b = 255; break;
                case 64: r = 95; g = 135; b = 0; break;
                case 65: r = 95; g = 135; b = 95; break;
                case 66: r = 95; g = 135; b = 135; break;
                case 67: r = 95; g = 135; b = 175; break;
                case 68: r = 95; g = 135; b = 215; break;
                case 69: r = 95; g = 135; b = 255; break;
                case 70: r = 95; g = 175; b = 0; break;
                case 71: r = 95; g = 175; b = 95; break;
                case 72: r = 95; g = 175; b = 135; break;
                case 73: r = 95; g = 175; b = 175; break;
                case 74: r = 95; g = 175; b = 215; break;
                case 75: r = 95; g = 175; b = 255; break;
                case 76: r = 95; g = 215; b = 0; break;
                case 77: r = 95; g = 215; b = 95; break;
                case 78: r = 95; g = 215; b = 135; break;
                case 79: r = 95; g = 215; b = 175; break;
                case 80: r = 95; g = 215; b = 215; break;
                case 81: r = 95; g = 215; b = 255; break;
                case 82: r = 95; g = 255; b = 0; break;
                case 83: r = 95; g = 255; b = 95; break;
                case 84: r = 95; g = 255; b = 135; break;
                case 85: r = 95; g = 255; b = 175; break;
                case 86: r = 95; g = 255; b = 215; break;
                case 87: r = 95; g = 255; b = 255; break;
                case 88: r = 135; g = 0; b = 0; break;
                case 89: r = 135; g = 0; b = 95; break;
                case 90: r = 135; g = 0; b = 135; break;
                case 91: r = 135; g = 0; b = 175; break;
                case 92: r = 135; g = 0; b = 215; break;
                case 93: r = 135; g = 0; b = 255; break;
                case 94: r = 135; g = 95; b = 0; break;
                case 95: r = 135; g = 95; b = 95; break;
                case 96: r = 135; g = 95; b = 135; break;
                case 97: r = 135; g = 95; b = 175; break;
                case 98: r = 135; g = 95; b = 215; break;
                case 99: r = 135; g = 95; b = 255; break;
                case 100: r = 135; g = 135; b = 0; break;
                case 101: r = 135; g = 135; b = 95; break;
                case 102: r = 135; g = 135; b = 135; break;
                case 103: r = 135; g = 135; b = 175; break;
                case 104: r = 135; g = 135; b = 215; break;
                case 105: r = 135; g = 135; b = 255; break;
                case 106: r = 135; g = 175; b = 0; break;
                case 107: r = 135; g = 175; b = 95; break;
                case 108: r = 135; g = 175; b = 135; break;
                case 109: r = 135; g = 175; b = 175; break;
                case 110: r = 135; g = 175; b = 215; break;
                case 111: r = 135; g = 175; b = 255; break;
                case 112: r = 135; g = 215; b = 0; break;
                case 113: r = 135; g = 215; b = 95; break;
                case 114: r = 135; g = 215; b = 135; break;
                case 115: r = 135; g = 215; b = 175; break;
                case 116: r = 135; g = 215; b = 215; break;
                case 117: r = 135; g = 215; b = 255; break;
                case 118: r = 135; g = 255; b = 0; break;
                case 119: r = 135; g = 255; b = 95; break;
                case 120: r = 135; g = 255; b = 135; break;
                case 121: r = 135; g = 255; b = 175; break;
                case 122: r = 135; g = 255; b = 215; break;
                case 123: r = 135; g = 255; b = 255; break;
                case 124: r = 175; g = 0; b = 0; break;
                case 125: r = 175; g = 0; b = 95; break;
                case 126: r = 175; g = 0; b = 135; break;
                case 127: r = 175; g = 0; b = 175; break;
                case 128: r = 175; g = 0; b = 215; break;
                case 129: r = 175; g = 0; b = 255; break;
                case 130: r = 175; g = 95; b = 0; break;
                case 131: r = 175; g = 95; b = 95; break;
                case 132: r = 175; g = 95; b = 135; break;
                case 133: r = 175; g = 95; b = 175; break;
                case 134: r = 175; g = 95; b = 215; break;
                case 135: r = 175; g = 95; b = 255; break;
                case 136: r = 175; g = 135; b = 0; break;
                case 137: r = 175; g = 135; b = 95; break;
                case 138: r = 175; g = 135; b = 135; break;
                case 139: r = 175; g = 135; b = 175; break;
                case 140: r = 175; g = 135; b = 215; break;
                case 141: r = 175; g = 135; b = 255; break;
                case 142: r = 175; g = 175; b = 0; break;
                case 143: r = 175; g = 175; b = 95; break;
                case 144: r = 175; g = 175; b = 135; break;
                case 145: r = 175; g = 175; b = 175; break;
                case 146: r = 175; g = 175; b = 215; break;
                case 147: r = 175; g = 175; b = 255; break;
                case 148: r = 175; g = 215; b = 0; break;
                case 149: r = 175; g = 215; b = 95; break;
                case 150: r = 175; g = 215; b = 135; break;
                case 151: r = 175; g = 215; b = 175; break;
                case 152: r = 175; g = 215; b = 215; break;
                case 153: r = 175; g = 215; b = 255; break;
                case 154: r = 175; g = 255; b = 0; break;
                case 155: r = 175; g = 255; b = 95; break;
                case 156: r = 175; g = 255; b = 135; break;
                case 157: r = 175; g = 255; b = 175; break;
                case 158: r = 175; g = 255; b = 215; break;
                case 159: r = 175; g = 255; b = 255; break;
                case 160: r = 215; g = 0; b = 0; break;
                case 161: r = 215; g = 0; b = 95; break;
                case 162: r = 215; g = 0; b = 135; break;
                case 163: r = 215; g = 0; b = 175; break;
                case 164: r = 215; g = 0; b = 215; break;
                case 165: r = 215; g = 0; b = 255; break;
                case 166: r = 215; g = 95; b = 0; break;
                case 167: r = 215; g = 95; b = 95; break;
                case 168: r = 215; g = 95; b = 135; break;
                case 169: r = 215; g = 95; b = 175; break;
                case 170: r = 215; g = 95; b = 215; break;
                case 171: r = 215; g = 95; b = 255; break;
                case 172: r = 215; g = 135; b = 0; break;
                case 173: r = 215; g = 135; b = 95; break;
                case 174: r = 215; g = 135; b = 135; break;
                case 175: r = 215; g = 135; b = 175; break;
                case 176: r = 215; g = 135; b = 215; break;
                case 177: r = 215; g = 135; b = 255; break;
                case 178: r = 215; g = 175; b = 0; break;
                case 179: r = 215; g = 175; b = 95; break;
                case 180: r = 215; g = 175; b = 135; break;
                case 181: r = 215; g = 175; b = 175; break;
                case 182: r = 215; g = 175; b = 215; break;
                case 183: r = 215; g = 175; b = 255; break;
                case 184: r = 215; g = 215; b = 0; break;
                case 185: r = 215; g = 215; b = 95; break;
                case 186: r = 215; g = 215; b = 135; break;
                case 187: r = 215; g = 215; b = 175; break;
                case 188: r = 215; g = 215; b = 215; break;
                case 189: r = 215; g = 215; b = 255; break;
                case 190: r = 215; g = 255; b = 0; break;
                case 191: r = 215; g = 255; b = 95; break;
                case 192: r = 215; g = 255; b = 135; break;
                case 193: r = 215; g = 255; b = 175; break;
                case 194: r = 215; g = 255; b = 215; break;
                case 195: r = 215; g = 255; b = 255; break;
                case 196: r = 255; g = 0; b = 0; break;
                case 197: r = 255; g = 0; b = 95; break;
                case 198: r = 255; g = 0; b = 135; break;
                case 199: r = 255; g = 0; b = 175; break;
                case 200: r = 255; g = 0; b = 215; break;
                case 201: r = 255; g = 0; b = 255; break;
                case 202: r = 255; g = 95; b = 0; break;
                case 203: r = 255; g = 95; b = 95; break;
                case 204: r = 255; g = 95; b = 135; break;
                case 205: r = 255; g = 95; b = 175; break;
                case 206: r = 255; g = 95; b = 215; break;
                case 207: r = 255; g = 95; b = 255; break;
                case 208: r = 255; g = 135; b = 0; break;
                case 209: r = 255; g = 135; b = 95; break;
                case 210: r = 255; g = 135; b = 135; break;
                case 211: r = 255; g = 135; b = 175; break;
                case 212: r = 255; g = 135; b = 215; break;
                case 213: r = 255; g = 135; b = 255; break;
                case 214: r = 255; g = 175; b = 0; break;
                case 215: r = 255; g = 175; b = 95; break;
                case 216: r = 255; g = 175; b = 135; break;
                case 217: r = 255; g = 175; b = 175; break;
                case 218: r = 255; g = 175; b = 215; break;
                case 219: r = 255; g = 175; b = 255; break;
                case 220: r = 255; g = 215; b = 0; break;
                case 221: r = 255; g = 215; b = 95; break;
                case 222: r = 255; g = 215; b = 135; break;
                case 223: r = 255; g = 215; b = 175; break;
                case 224: r = 255; g = 215; b = 215; break;
                case 225: r = 255; g = 215; b = 255; break;
                case 226: r = 255; g = 255; b = 0; break;
                case 227: r = 255; g = 255; b = 95; break;
                case 228: r = 255; g = 255; b = 135; break;
                case 229: r = 255; g = 255; b = 175; break;
                case 230: r = 255; g = 255; b = 215; break;
                case 231: r = 255; g = 255; b = 255; break;
                case 232: r = 8; g = 8; b = 8; break;
                case 233: r = 18; g = 18; b = 18; break;
                case 234: r = 28; g = 28; b = 28; break;
                case 235: r = 38; g = 38; b = 38; break;
                case 236: r = 48; g = 48; b = 48; break;
                case 237: r = 58; g = 58; b = 58; break;
                case 238: r = 68; g = 68; b = 68; break;
                case 239: r = 78; g = 78; b = 78; break;
                case 240: r = 88; g = 88; b = 88; break;
                case 241: r = 96; g = 96; b = 96; break;
                case 242: r = 102; g = 102; b = 102; break;
                case 243: r = 118; g = 118; b = 118; break;
                case 244: r = 128; g = 128; b = 128; break;
                case 245: r = 138; g = 138; b = 138; break;
                case 246: r = 148; g = 148; b = 148; break;
                case 247: r = 158; g = 158; b = 158; break;
                case 248: r = 168; g = 168; b = 168; break;
                case 249: r = 178; g = 178; b = 178; break;
                case 250: r = 188; g = 188; b = 188; break;
                case 251: r = 198; g = 198; b = 198; break;
                case 252: r = 208; g = 208; b = 208; break;
                case 253: r = 218; g = 218; b = 218; break;
                case 254: r = 228; g = 228; b = 228; break;
                case 255: r = 238; g = 238; b = 238; break;

                default:
                    break;
            }
        }
    }
}
